package com.hm.health.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.goodix.utils.DensityUtil;
import java.util.Locale;

/**
 * 圆弧计分
 *
 * @author Administrator
 */
public class TemperatureView extends AppCompatTextView {
    // 最小高度
    public static final int MIN_HEIGHT = 200;
    // 渐变颜色
    public static final int[] sColors = new int[]{0XFF1E9600, 0XFFFFF200, 0XFFFF0000};
    // 黑色
    private static final int BLACK_COLOR = 0x70000000;
    // 白色
    private static final int WHITE_COLOR = 0xddffffff;
    public static final int TEMPERATURE_LOWER = 32;
    public static final int TEMPERATURE_UPPER = 42;
    public static final int INVALID_TEMPERATURE = -1;

    private Paint mPaintBlack, mPaintWhite, mPaintTemperature, mPaintTextUnits;
    private RectF mRect;
    private float mCurveWidth;
    private float mTemperature = 32;
    private float mSwapAngle = 180;
    private LinearGradient mLinearGradient;
    private boolean mIsChanged;

    public TemperatureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(mTemperature);
    }

    public void init(float score) {
        this.mTemperature = score;
        mCurveWidth = DensityUtil.dip2px(getContext(), 12);

        mPaintBlack = new Paint();
        mPaintBlack.setAntiAlias(true);
        mPaintBlack.setColor(BLACK_COLOR);
        mPaintBlack.setStrokeWidth(mCurveWidth * 1f);
        mPaintBlack.setStyle(Style.STROKE);

        mPaintWhite = new Paint();
        mPaintWhite.setAntiAlias(true);
        mPaintWhite.setColor(WHITE_COLOR);
        mPaintWhite.setColor(Color.rgb(244, 30, 8));
        mPaintWhite.setTextSize(mCurveWidth *6.0f);
        mPaintWhite.setStrokeWidth(mCurveWidth * 1f);
        mPaintWhite.setTextAlign(Align.CENTER);
        mPaintWhite.setStyle(Style.STROKE);

        mPaintTemperature = new Paint();
        mPaintTemperature.setAntiAlias(true);
        mPaintTemperature.setColor(Color.BLACK);
        mPaintTemperature.setTextSize(DensityUtil.dip2px(getContext(), 28));
        mPaintTemperature.setStrokeWidth(mCurveWidth * 0.2f);
        mPaintTemperature.setTextAlign(Align.CENTER);
        mPaintTemperature.setStyle(Style.FILL);

        mPaintTextUnits = new Paint();
        mPaintTextUnits.setAntiAlias(true);
        mPaintTextUnits.setColor(Color.GRAY);
        mPaintTextUnits.setTextSize(DensityUtil.dip2px(getContext(), 14));
        mPaintTextUnits.setStrokeWidth(mCurveWidth * 0.2f);
        mPaintTextUnits.setTextAlign(Align.CENTER);
        mPaintTextUnits.setStyle(Style.FILL);
    }

    protected void onDraw(Canvas c) {
        super.onDraw(c);

        float w = getWidth();
        float h = getHeight() * 2;

        float rectl = (w  > h) ? h: w;

        rectl -= mCurveWidth * 4;
        float left = (w - rectl) / 2;
        float top = (h - rectl) / 2 - mCurveWidth * 3;
        float right = left + rectl;
        float bottom = top + rectl;
        if (null == mRect || mIsChanged) {
            mRect = new RectF(left, top, right, bottom);
        }

        if (null == mLinearGradient || mIsChanged) {
            mLinearGradient = new LinearGradient(left, 0, right, 0,
                    sColors, new float[]{0.53f, 0.68f,1f}, Shader.TileMode.CLAMP);
        }

        mIsChanged = false;

        mPaintWhite.setShader(mLinearGradient);

        float angle = 0;
        if (mTemperature != INVALID_TEMPERATURE) {
            if (mTemperature < TEMPERATURE_LOWER) {
                angle = 0;
            } else if(mTemperature >= TEMPERATURE_UPPER) {
                angle = 180;
            } else {
                angle = (mTemperature - TEMPERATURE_LOWER) * (mSwapAngle / 10);
            }
        }

        c.drawArc(mRect, 180, mSwapAngle, false, mPaintBlack);
        c.drawArc(mRect, 180, angle, false, mPaintWhite);

        String value = String.format(Locale.ENGLISH, "%.2f  ", mTemperature);
        float textWidth = mPaintTemperature.measureText(value);

        if (mTemperature < 37.3) {
            mPaintTemperature.setColor(sColors[0]);
        } else if(mTemperature >= 37.3 && mTemperature <38.7) {
            mPaintTemperature.setColor(0xffffa500);
        } else if(mTemperature >= 38.7) {
            mPaintTemperature.setColor(sColors[2]);
        }

        if (mTemperature != -1) {
            String units = "℃";
            c.drawText(value, w / 2, mRect.top + mRect.height() / 2, mPaintTemperature);
            c.drawText(units, w / 2 + textWidth / 2, mRect.top + mRect.height() / 2, mPaintTextUnits);
        } else {
            mPaintTemperature.setColor(BLACK_COLOR);
            c.drawText("---", w / 2, mRect.top + mRect.height() / 2, mPaintTemperature);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int hMode = View.MeasureSpec.getMode(heightMeasureSpec);
        int wSize = View.MeasureSpec.getSize(widthMeasureSpec);

        if (hMode == View.MeasureSpec.AT_MOST) {
            setMeasuredDimension(wSize, DensityUtil.dip2px(getContext(), MIN_HEIGHT));
        }

        mIsChanged = true;
    }

    public void setTemperature(float temperature) {

        if (temperature == INVALID_TEMPERATURE) {
            mTemperature =  INVALID_TEMPERATURE;
        } else {
            mTemperature = temperature;
        }

    }
}
